package com.zhuchaooasystem.presenter.common;

import android.app.Activity;
import android.database.Cursor;
import android.provider.MediaStore;

import com.zhuchaooasystem.base.BasePresenter;

import java.util.ArrayList;

/**
 * @author yinsi
 */


public class LocalAlbumPresenter extends BasePresenter<LocalAlbumContract.View> implements LocalAlbumContract.Presenter {

    public LocalAlbumPresenter(Activity activity, LocalAlbumContract.View view) {
        super(activity, view);
    }

    //----
    //获取本地存储图片的字段
    private final String[] IMAGE_PROJECTION = {
            MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Images.Media._ID};


    @Override
    public void getData() {

        showLoading();
        //----
        Cursor cursor = null;
        try {
            cursor = mActivity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_PROJECTION, null, null, IMAGE_PROJECTION[1] + " DESC");
            if (null != cursor) {
                //数量大于0
                if (cursor.getCount() > 0) {
                    ArrayList<String> imageList = new ArrayList<>();
                    cursor.moveToFirst();
                    do {
                        imageList.add(cursor.getString(cursor.getColumnIndexOrThrow(IMAGE_PROJECTION[0])));
                    } while (cursor.moveToNext());
                    mView.refreshData(imageList);
                } else {
                    //获取数量为0
                    mView.getEmpty();
                }
            } else {
                //获取失败
                mView.getError();
            }
        } catch (Exception e) {
        } finally {
            LoadingDiaogDismiss();
            try {
                if (null != cursor) {
                    cursor.close();
                }
            } catch (Exception ex) {

            }
        }

    }
}
